package de.cbf.cam.dsa.entity.edit.item.act;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.ui.IPartListener2;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

import de.cbf.cam.dsa.entity.edit.item.controller.EditItemsMainController;
import de.cbf.cam.dsa.entity.edit.item.resolver.service_resolver.IServiceResolver_EntityItem;
import de.cbf.cam.dsa.entity.edit.talent.resolver.service_resolver.IServiceResolver_EntityTalent;
import de.cbf.util.entity.edit.controller.AbstractActivatorEntityEdit;
import de.cbf.util.entity.facet.persistence.resolver.IServiceResolverEntity;

/**
 * The activator class controls the plug-in life cycle
 */
public class ActivatorEditEntityItem extends AbstractActivatorEntityEdit {
	private static final Log logger = LogFactory.getLog(ActivatorEditEntityItem.class);
	// The plug-in ID
	public static final String PLUGIN_ID = "de.cbf.cam.dsa.entity.edit.item"; //$NON-NLS-1$
	
	public static final String LOG_VIEW_ITEM_EDIT_SEC_ID = "EditItemLogViewSecondaryId";

	// The shared instance
	private static ActivatorEditEntityItem plugin;	
	
	protected IServiceResolver_EntityTalent serviceResolverEntityTalent;
	
	/**
	 * The constructor
	 */
	public ActivatorEditEntityItem() {
		super();
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static ActivatorEditEntityItem getDefault() {
		return plugin;
	}

	@Override
	protected IPartListener2 getMainControllerInstance() {
		return EditItemsMainController.getInstance();		
	}

	@Override
	protected Class<? extends IServiceResolverEntity> getServiceEntityResolverClass() {
		return IServiceResolver_EntityItem.class;
	}

	@Override
	protected void setPluginInstance(AbstractActivatorEntityEdit activatorFacet) {
		if (activatorFacet instanceof ActivatorEditEntityItem) {
			ActivatorEditEntityItem activator = (ActivatorEditEntityItem) activatorFacet;
			plugin= activator;
		} else {
			String err_msg = "entity of wrong type(expected "+ ActivatorEditEntityItem.class.getSimpleName() + "): " + (activatorFacet == null ? "null"
					: activatorFacet.getClass().getSimpleName());
			logger.error(err_msg);
		}	
	}
	
	protected void obtainEntityResolvers(BundleContext context) {
		super.obtainEntityResolvers(context);
		registerTrackersForEntityTalentResolverService(context);
		acquireEntityTalentResolverService(context);//we provide view to edit facet entity, so we need the controllerLogView for this		
	}
	
	@Override
	protected String getPlugin_ID() {		
		return PLUGIN_ID;
	}
	
	protected void registerTrackersForEntityTalentResolverService(BundleContext context) {
		//add listener for service changes of entity resolver services
		STC_ResolverEntity customizer = new STC_ResolverEntity(context);
		String className = getServiceEntityResolverClass().getName();		
		serviceTrackerResolverEntity = new ServiceTracker<IServiceResolverEntity,Object>(context, className , customizer); 
		serviceTrackerResolverEntity.open();		
	}

	protected void acquireEntityTalentResolverService(BundleContext context) {
		ServiceReference<? extends IServiceResolver_EntityTalent> reference = context.getServiceReference(IServiceResolver_EntityTalent.class);
		IServiceResolver_EntityTalent service = context.getService(reference);
		serviceResolverEntityTalent = service;		
	}
	
	
	public IServiceResolverEntity getServiceResolverEntity() {
		return serviceResolverEntity;
	}
	
	public IServiceResolver_EntityTalent getServiceResolverEntityTalent() {
		return serviceResolverEntityTalent;
	}
	
	protected class STC_ResolverEntityTalent implements ServiceTrackerCustomizer<IServiceResolver_EntityTalent,Object>{
		
		protected BundleContext stcContext;
		
		
		public STC_ResolverEntityTalent(BundleContext stcContext) {
			super();
			this.stcContext = stcContext;
		}

		@Override
		public Object addingService(ServiceReference<IServiceResolver_EntityTalent> reference) {
			IServiceResolver_EntityTalent service = stcContext.getService(reference);
			serviceResolverEntityTalent = service;
			return service;
		}

		@Override
		public void modifiedService(ServiceReference<IServiceResolver_EntityTalent> reference,
				Object service) {
			if (service instanceof IServiceResolver_EntityTalent) {
				IServiceResolver_EntityTalent serviceResolver = (IServiceResolver_EntityTalent) service;
				serviceResolverEntityTalent = serviceResolver;
			} else {
				String err_msg = "entity of wrong type(expected "
						+ IServiceResolver_EntityTalent.class.getSimpleName() + "): " + (service == null ? "null"
						: service.getClass().getSimpleName());
				logger.error(err_msg);
			}		
		}

		@Override
		public void removedService(ServiceReference<IServiceResolver_EntityTalent> reference,Object service) {
			serviceResolverEntityTalent = null;			
		}
	}

	
}
